package com.wfs.suanfa.nio.xiaoliu;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author wfs
 * @date 2021/3/28 21:31
 */
public class TCPServer {
    public static void main(String[] args) {
        try {
            ServerSocket ss=new ServerSocket(6666);
            while (true){
                Socket s = ss.accept();
                System.out.println("A client connected");
                DataInputStream dis = new DataInputStream(s.getInputStream());
                DataOutputStream dos = new DataOutputStream(s.getOutputStream());
                String str=null;
                if ( (str = dis.readUTF()) !=null){
                    System.out.println(str);
                    System.out.println("from "+ s.getInetAddress()+" ,port:"+s.getPort());
                }
                dos.writeUTF("Hello "+s.getInetAddress()+" ,port:"+s.getPort());
                dis.close();
                dos.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
